package com.crypto.ui.components;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.AbstractLink;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.PropertyModel;

import com.crypto.ui.action.Action;

public class BoxPanel extends Panel {

	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unused")
	private String actionResponseText = "";
	
	private Label actionResponseLabel;
	
	public BoxPanel(String id, String title, String url, String responseTitle, final Action action) {
		super(id);
		init(title, url, responseTitle);
		add(new AjaxFallbackLink<String>("box-panel-action") {
			private static final long serialVersionUID = 1L;
			@Override
			public void onClick(AjaxRequestTarget target) {
				try {
					BoxPanel.this.actionResponseText = action.execute();
					if(target != null) 
						target.add(actionResponseLabel);
				} catch (Exception e) {
					error(e.getMessage());
				}
			}
		});
	}
	
	public BoxPanel(String id, String title, String url, String responseTitle, AbstractLink link) {
		super(id);
		init(title, url, responseTitle);
		add(link);
	}

	private void init(String title, String url, String responseTitle) {
		
		add(new Label("box-panel-title", title));
		add(new Label("box-panel-url", url).setEscapeModelStrings(false));
		add(new Label("box-panel-action-response-title", responseTitle));
		
		actionResponseLabel = new Label("box-panel-action-response", new PropertyModel<String>(this, "actionResponseText"));
		actionResponseLabel.setOutputMarkupId(true);
		add(actionResponseLabel.setEscapeModelStrings(false));
	}
	
	public void setActionResponseText(String actionResponseText) {
		this.actionResponseText = actionResponseText;
	}
	
}
